package detailsdb.dao.impl;

import detailsdb.dao.BaseDao;
import detailsdb.dao.DetailsdbDao;
import detailsdb.entity.Detailsdb;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DetailsdbDaoImpl extends BaseDao implements DetailsdbDao {
    @Override
    public List<Detailsdb> getList(String ordernumber) {
        String sql="select Detailsid,snacksid,Orderbiaoid,originAmount,price,name,number,puserstime from Detailsdb where Orderbiaoid=(select id from Orderbiao where ordernumber =?)";
        ResultSet rs=this.executeQuery(sql,ordernumber);
        List<Detailsdb> list=new ArrayList<>();

        try {
            while (rs.next()){
                list.add(new Detailsdb(rs.getInt(1),rs.getInt(2),rs.getInt(3),
                        rs.getFloat(4),rs.getFloat(5),rs.getString(6),
                        rs.getInt(7),rs.getString(8)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            this.closeConnection(null,null,rs);
        }
        return list;
    }

    @Override
    public int UpDetails(int id) {
        String sql="UPDATE Detailsdb set puserstime=NOW() where Orderbiaoid=(select id from Orderbiao where id=?)";
        return this.executeUpdate(sql,id);
    }

    @Override
    public int getCount() {
        String sql="select count(*) from Detailsdb";
        ResultSet rs=this.executeQuery(sql);
        try {
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            closeConnection(null,null,rs);
        }
        return 0;
    }
}
